package com.neusoft.nep.NEPM.controller;
import com.neusoft.nep.entity.ProvinceData;
import com.neusoft.nep.entity.nepgResult;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FXMLUtils;
import com.neusoft.tools.FileUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import java.io.IOException;
import java.util.*;
public class ProvinceDataTotalStatisViewController {
    @FXML
    private TreeView treeview;
    @FXML
    private Button btn_out;
    @FXML
    private TreeItem To_AqiTrendTotalStatisView;
    @FXML
    private TreeItem To_AqiSeperatedStatisView;
    @FXML
    private TreeItem To_publicAqiDataList;
    @FXML
    private TreeItem To_OtherStatisView;
    @FXML
    private TreeItem To_ProvinceDataTotalStatisView;
    @FXML
    private TableView tableView;
    @FXML
    private TreeItem To_confirmDataView;
    @FXML
    private TableColumn col_province;
    @FXML
    private TableColumn col__province;
    @FXML
    private TableColumn col_numprovince;
    @FXML
    private TableColumn col_so2;
    @FXML
    private TableColumn col_co;
    @FXML
    private TableColumn col_pm25;
    @FXML
    private TableColumn col_aqi;
    private ObservableList list= FXCollections.observableArrayList();
    private Map provincemap;
    @FXML
    public void initialize() throws IOException {
        treeview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == To_AqiSeperatedStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiSeperatedStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_AqiTrendTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiTrendTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_OtherStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/OtherStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_ProvinceDataTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/ProvinceDataTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }

            }
            else if (newValue == To_confirmDataView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/confirmDataView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }else if (newValue == To_publicAqiDataList) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataList.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
        });
        col__province.setCellValueFactory(new PropertyValueFactory<>("Province"));
        col_province.setCellValueFactory(new PropertyValueFactory<>("_Province"));
        col_numprovince.setCellValueFactory(new PropertyValueFactory<>("Province_num"));
        col_so2.setCellValueFactory(new PropertyValueFactory<>("SO2"));
        col_co.setCellValueFactory(new PropertyValueFactory<>("CO"));
        col_pm25.setCellValueFactory(new PropertyValueFactory<>("PM25"));
        col_aqi.setCellValueFactory(new PropertyValueFactory<>("AQI"));
        col__province.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_province.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_numprovince.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_so2.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_co.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_pm25.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));
        col_aqi.prefWidthProperty().bind(tableView.widthProperty().multiply(0.1));

    List<nepgResult> RecordData=new ArrayList();
    RecordData=FileUtils.readListObject("NEPGresult.txt");
    query_Province();
        for(nepgResult item:RecordData) {
            nepgResult nepgresult = item;
            String province = nepgresult.getProvince();
            ProvinceData provinceData = (ProvinceData) provincemap.get(province);
            int num_so2 = provinceData.getSO2();
            int num_co = provinceData.getCO();
            int num_pm25 = provinceData.getPM25();
            int num_aqi = provinceData.getAQI();
            int so2= nepgresult.getSo2();
            int co = nepgresult.getCo();
            int pm25 = nepgresult.getPm25();
            String aqi = nepgresult.getRealLevel();
            if(so2>475){
                num_so2++;
                provinceData.setSO2(num_so2);
            }
            if(co>35){
                num_co++;
                provinceData.setCO(num_co);
            }
            if(pm25>115){
                num_pm25++;
                provinceData.setPM25(num_pm25);
            }
            if(aqi=="三" || aqi=="四" || aqi=="五"||aqi=="六"){
                num_aqi++;
                provinceData.setAQI(num_aqi);
            }
        }
        list.addAll(provincemap.values());
        tableView.setItems(list);

}
    public void Log_out() throws IOException {
        FXMLUtils.switchToShow(btn_out,"NEPM/view/LoginView.fxml");
    }
    public void back() throws IOException {
        FXMLUtils.switchToShow(btn_out, "NEPM/view/ManagerUIView.fxml");
    }
    public void query_Province(){
        provincemap=new LinkedHashMap<String, ProvinceData>();
        provincemap.put("北京市",new ProvinceData("北京市","京",1,0,0,0,0));
        provincemap.put("天津市",new ProvinceData("天津市","津",2,0,0,0,0));
        provincemap.put("上海市",new ProvinceData("上海市","沪",3,0,0,0,0));
        provincemap.put("重庆市",new ProvinceData("重庆市","渝",4,0,0,0,0));
        provincemap.put("河北省",new ProvinceData("河北省","冀",5,0,0,0,0));
        provincemap.put("河南省",new ProvinceData("河南省","豫",6,0,0,0,0));
        provincemap.put("山东省",new ProvinceData("山东省","鲁",7,0,0,0,0));
        provincemap.put("山西省",new ProvinceData("山西省","晋",8,0,0,0,0));
        provincemap.put("浙江省",new ProvinceData("浙江省","浙",9,0,0,0,0));
        provincemap.put("安徽省",new ProvinceData("安徽省","皖",10,0,0,0,0));
        provincemap.put("四川省",new ProvinceData("四川省","川",11,0,0,0,0));
        provincemap.put("辽宁省",new ProvinceData("辽宁省","辽",12,0,0,0,0));
        provincemap.put("吉林省",new ProvinceData("吉林省","吉",13,0,0,0,0));
        provincemap.put("黑龙江省",new ProvinceData("黑龙江省","黑",14,0,0,0,0));
        provincemap.put("湖北省",new ProvinceData("湖北省","鄂",15,0,0,0,0));
        provincemap.put("湖南省",new ProvinceData("湖南省","湘",16,0,0,0,0));
        provincemap.put("江西省",new ProvinceData("江西省","赣",17,0,0,0,0));
        provincemap.put("福建省",new ProvinceData("福建省","闽",18,0,0,0,0));
        provincemap.put("广东省",new ProvinceData("广东省","粤",19,0,0,0,0));
        provincemap.put("江苏省",new ProvinceData("江苏省","苏",20,0,0,0,0));
        provincemap.put("甘肃省",new ProvinceData("甘肃省","甘",21,0,0,0,0));
        provincemap.put("青海省",new ProvinceData("青海省","青",22,0,0,0,0));
        provincemap.put("贵州省",new ProvinceData("贵州省","贵",23,0,0,0,0));
        provincemap.put("云南省",new ProvinceData("云南省","云",24,0,0,0,0));
        provincemap.put("陕西省",new ProvinceData("陕西省","陕",25,0,0,0,0));
        provincemap.put("内蒙古自治区",new ProvinceData("内蒙古自治区","蒙",26,0,0,0,0));
        provincemap.put("新疆维吾尔自治区",new ProvinceData("新疆维吾尔自治区","新",27,0,0,0,0));
        provincemap.put("西藏自治区",new ProvinceData("西藏自治区","藏",28,0,0,0,0));
        provincemap.put("宁夏回族自治区",new ProvinceData("宁夏回族自治区","宁",29,0,0,0,0));
        provincemap.put("广西壮族自治区",new ProvinceData("广西壮族自治区","桂",30,0,0,0,0));
        provincemap.put("台湾省",new ProvinceData("台湾省","台",31,0,0,0,0));
        provincemap.put("香港特别行政区",new ProvinceData("香港特别行政区","港",32,0,0,0,0));
        provincemap.put("澳门特别行政区",new ProvinceData("澳门特别行政区","澳",33,0,0,0,0));
        provincemap.put("海南省",new ProvinceData("海南省","琼",34,0,0,0,0));
    }
}
